
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: AbstractSchemaUpdate.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.schema.change;

import org.dellroad.sidekar.Sidekar;
import org.dellroad.sidekar.schema.model.EntityModel;
import org.dellroad.sidekar.schema.model.SchemaModel;

/**
 * {@link SchemaChange} that removes an index from some entity in the schema.
 */
public class RemoveIndexChange extends EntityChange {

    private final String indexName;

    /**
     * Constructor.
     *
     * @param entityName name of the entity from which the index should be removed
     * @param indexName name of the index to be removed
     * @throws IllegalArgumentException if {@code entityName} is not a legal entity name
     * @throws IllegalArgumentException if {@code indexName} is not a legal index name
     */
    public RemoveIndexChange(String entityName, String indexName) {
        super(entityName);
        Sidekar.checkValidIndexName(indexName);
        this.indexName = indexName;
    }

    public String getIndexName() {
        return this.indexName;
    }

    @Override
    public String getDescription() {
        return "remove index `" + this.indexName + "' from entity `" + this.getEntityName() + "'";
    }

    @Override
    public void checkValidFor(SchemaModel schemaModel) {
        super.checkValidFor(schemaModel);
        final EntityModel entityModel = this.checkEntityExists(schemaModel, this.getEntityName());
        this.checkIndexExists(entityModel, this.indexName);
    }

    @Override
    public final void visit(SchemaChangeSwitch schemaChangeSwitch) {
        schemaChangeSwitch.caseRemoveIndexChange(this);
    }
}

